/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/*

#asset(qx/icon/${qx.icontheme}/22/actions/view-refresh.png)
#asset(qx/icon/${qx.icontheme}/22/actions/document-new.png)
#asset(qx/icon/${qx.icontheme}/22/actions/dialog-close.png)
#asset(qx/icon/${qx.icontheme}/22/actions/document-save.png)

#asset(qx/icon/${qx.icontheme}/22/actions/system-search.png)

#asset(qx/icon/${qx.icontheme}/16/mimetypes/text-plain.png)
#asset(qx/icon/${qx.icontheme}/16/mimetypes/media-image.png)

*/

/**
 * List manager view class
 */
qx.Class.define("webapp.view.FileList", {

	extend : qx.ui.container.Composite,
  
  construct : function()
  {
    this.base(arguments);

    this.set({
      layout        : new qx.ui.layout.VBox(),
      decorator     : "main",
      allowStretchX : false
    });

    this.add(this._createToolbar());
    
    var list = this._createList();
    list.set({
      width  : 400,
      height : 300
    });
    this.add(list);
  },
  
  members : {
    
    _list       : null,
    _search     : null,
    _filter     : null,
    _controller : null,
    _buttons    : null,
    

    _createToolbar : function()
    {
      this._toolbar = new qx.ui.toolbar.ToolBar();
      this._buttons = {};
      
      var reload = new qx.ui.toolbar.Button(this.tr("Reload"), "icon/22/actions/view-refresh.png");
      reload.setMarginLeft(2);
      this.addButton("reload", reload);
      
      var create = new qx.ui.toolbar.Button(this.tr("Create"), "icon/22/actions/document-new.png");
      this.addButton("create", create);
      
      var download = new qx.ui.toolbar.Button(this.tr("Download"), "icon/22/actions/document-save.png");
      this.addButton("download", download);

      var remove = new qx.ui.toolbar.Button(this.tr("Remove"), "icon/22/actions/dialog-close.png");
      this.addButton("remove", remove);
      
      this._toolbar.add(new qx.ui.core.Spacer(), {flex : 1});
      
      var decorator = new qx.ui.decoration.Uniform();
      decorator.set({
        style            : "solid",
        width            : 1,
        color            : "group-border",
        backgroundColor  : "white",
        backgroundImage  : "icon/22/actions/system-search.png",
        backgroundRepeat : "no-repeat"
      });
      this._search = new qx.ui.form.TextField();
      this._search.set({
        width       : 200,
        liveUpdate  : true,
        decorator   : decorator,
        padding     : [3, 2, 3, 23],
        margin      : [6, 4, 4, 4],
        placeholder : this.tr("File name")
      });
      this._toolbar.add(this._search);
      
      this._toolbar.setShow("icon");

      return this._toolbar;
    },
    
    addButton : function(name, button, position)
    {
      if(position != null)
      {
        this._toolbar.addAt(button, position);
      }
      else
      {
        this._toolbar.add(button);
      }

      this._buttons[name] = button;
      
      button.setToolTipText(button.getLabel());
    },

    getButtons : function()
    {
      return this._buttons;
    },
    
  	_createList : function()
    {
      this._list = new qx.ui.form.List();
      this._list.setAppearance("widget");
      
      this._controller = new qx.data.controller.List(null, this._list, "name");
      this._controller.setIconOptions({converter : function(value) 
      {
        var parts     = value.split(".");
        var extension = parts[parts.length - 1]; 
        
        var result = "webapp/empty-x-generic.png";
        switch(extension)
        {
          case "txt":
          case "conf":
            result = "icon/16/mimetypes/text-plain.png";
            break;
          case "jpg":
          case "jpeg":
          case "png":
          case "gif":
            result = "icon/16/mimetypes/media-image.png";
            break;
        }
        
        return result;
      }});
      this._controller.setIconPath("name");
      this._controller.setDelegate({filter : qx.lang.Function.bind(function(item)
      {
        return !this._filter || item.getName().toLowerCase().indexOf(this._filter) != -1;
      }, this)});
      this._search.addListener("changeValue", function(event)
      {
        this._filter = event.getData() ? event.getData().toLowerCase() : null; 
        this._controller.update();
      }, this);
      
      
      return this._list;
    },

    getListController : function()
    {
      return this._controller;
    }

  }

});